package reduce.fz.com.reduce.init;

import android.content.Context;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.LinearLayout;

import reduce.fz.com.reduce.custom.HorizontalLinearLayoutManager;

/**
 * Created by qingshengzheng on 15/7/5.
 */
public class RecycleOper {
    private Context context;
    private RecyclerView recycleView;

    public LinearLayoutManager getLlm() {
        return llm;
    }

    private LinearLayoutManager llm;

    private GridLayoutManager gm;

    public RecycleOper(Context context, RecyclerView listView) {
        this.context = context;
        this.recycleView = listView;
    }

    public RecyclerView setDefault() {
        LinearLayoutManager mLayoutManager = new LinearLayoutManager(context);
        // 设置ItemAnimator
        recycleView.setItemAnimator(new DefaultItemAnimator());
        // 设置固定大小
        recycleView.setHasFixedSize(true);
        recycleView.setLayoutManager(mLayoutManager);
        return recycleView;
    }

    public RecyclerView setHorDefault() {
        HorizontalLinearLayoutManager llm = new HorizontalLinearLayoutManager(context);

        recycleView.setLayoutManager(llm);
        // 设置ItemAnimator
        recycleView.setItemAnimator(new DefaultItemAnimator());
        // 设置固定大小
        recycleView.setHasFixedSize(true);
        return recycleView;
    }

    public RecyclerView setGmDefault(int col) {
        GridLayoutManager llm = new GridLayoutManager(context,col);

        recycleView.setLayoutManager(llm);
        // 设置ItemAnimator
        recycleView.setItemAnimator(new DefaultItemAnimator());
        // 设置固定大小
        recycleView.setHasFixedSize(true);
        return recycleView;
    }




    public void setRecycleScrollListener(final RecycleTrigger recycleTrigger) {
        recycleView.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                int lastVisibleItem = llm.findLastVisibleItemPosition();
                int totalItemCount = llm.getItemCount();
                //lastVisibleItem >= totalItemCount - 4 表示剩下4个item自动加载
                // dy>0 表示向下滑动
                if (lastVisibleItem >= totalItemCount - 4 && dy > 0) {
                    recycleTrigger.deal();
                }
            }
        });


    }


    public abstract static class RecycleTrigger {
        public abstract void deal();
    }

}
